import { CdtMetaData, CdtMetaParam, Role, RoleParentId } from '@/types/role';
import apiClient from '../apiClient';

export enum RoleApi {
  GetConditionMetadata = '/openapi/v1/policies/conditions-metadata',
  // Role = '/openapi/v1/role',
  Role = '/openapi/v2/roles',
  UpdateContionParam = '/openapi/v1/policies/conditions-params',
}

/**
 * 获取规则条件元数据
 * @returns 规则条件元数据
 */
const getConditionMetaData = () =>
  apiClient.get<CdtMetaData>({ url: RoleApi.GetConditionMetadata });

/**
 * 更新关键字数据
 * @returns
 */
const updateConditionParam = (id: string, data: CdtMetaParam) =>
  apiClient.put<CdtMetaData>({ url: RoleApi.UpdateContionParam + `/${id}`, data });

/**
 * 批量更新关键字数据
 * @returns
 */
const updateConditionParams = (data: CdtMetaParam[]) =>
  apiClient.put<CdtMetaData>({ url: RoleApi.UpdateContionParam, data });

/**
 * 新增角色
 * @param data 角色
 * @returns
 */
const addRole = (data: Role & RoleParentId) => apiClient.post<Role>({ url: RoleApi.Role, data });

/**
 * 更新角色
 * @param data 角色
 * @returns
 */
const updateRole = (data: Role) => apiClient.put<Role>({ url: RoleApi.Role + `/${data.id}`, data });

/**
 * 删除角色
 * @param data 角色
 * @returns
 */
const delRole = (id: string) => apiClient.delete({ url: RoleApi.Role + `/${id}` });

export default {
  getConditionMetaData,
  updateConditionParam,
  updateConditionParams,
  addRole,
  updateRole,
  delRole,
};
